﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using JuCheap.Core.Infrastructure.Extentions;
using JuCheap.Core.Interfaces;
using JuCheap.Core.Models.Filters;
using JuCheap.Core.Models.ViewModels;
using JuCheap.Core.Web.Filters;
using JuCheap.Core.Web.Models;
using Microsoft.AspNetCore.Mvc;

namespace JuCheap.Core.Web.Controllers
{
    public class PaiBanController : Controller
    {
        private IRepository _iRepository;
        public PaiBanController(IRepository iRepository)
        {
            _iRepository = iRepository;
        }
        public IActionResult Index()
        {
            return View();
        }

        public IActionResult WorkIndex()
        {
            return View();
        }

        public async Task<IActionResult> Delete([FromBody]List<string> ids)
        {
            var result = new JsonResultModel<bool>();
            if (ids.AnyOne())
            {
                result.flag = true;
                foreach (string str in ids)
                {
                    await _iRepository.DeletePaiBan(Convert.ToInt32(str));
                }
            }
            return Json(result);
        }

        public IActionResult Add()
        {
            return View(new PaiBanViewModel());
        }


        public IActionResult Test()
        {
            return View(new PaiBanViewModel());
        }


        public async Task<IActionResult> EditWorkerPaiBan(int id)
        {
            var dto = await _iRepository.GetPaiBans(new PaiBanFilter() { id = id, });

            if (dto == null || dto.rows.Count == 0)
            {
                return RedirectToAction("Add");
            }
            return View(dto.rows[0]);
        }

        public IActionResult UpdateWorker(int workerId)
        {
            PaiBanViewModel model = new PaiBanViewModel();
            model.WorkerId = workerId;
            model.Worker =  _iRepository.GetWorkers(new WorkerFilter() {
                 id = workerId
            }).Result.rows[0];
            return View(model);
        }

        public async Task<IActionResult> Edit(int id)
        {
            var dto = await _iRepository.GetPaiBans(new PaiBanFilter() { id = id, });

            if (dto == null || dto.rows.Count == 0)
            {
                return RedirectToAction("Add");
            }
            return View(dto.rows[0]);
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit(PaiBanViewModel model)
        {
            if (ModelState.IsValid)
            {
                model.WorkerId = null;
                //model.Worker = new WorkerViewModel() { Id = model.WorkerId };
                //model.BanCi = new BanCiViewModel() { Id = model.BanCiId };
                var result = await _iRepository.UpdatePaiBan(model);
                return RedirectToAction("Index");
            }
            return View(model);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Add(PaiBanViewModel model)
        {
            if (ModelState.IsValid)
            {
                //model.Worker = new WorkerViewModel() { Id = model.WorkerId };
                //model.BanCi = new BanCiViewModel() { Id = model.BanCiId };
                var result = await _iRepository.AddPaiBan(model);
                //if (result.IsNotBlank())
                if(model.WorkerId != null)
                    return RedirectToAction("Index","Worker");
                return RedirectToAction("Index");
            }
            return View(model);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> EditWorkerPaiBan(PaiBanViewModel model)
        {
            if (ModelState.IsValid)
            {
                //model.Worker = new WorkerViewModel() { Id = model.WorkerId };
                //model.BanCi = new BanCiViewModel() { Id = model.BanCiId };
                var result = await _iRepository.UpdatePaiBan(model);
                //if (result.IsNotBlank())
                return RedirectToAction("WorkIndex");
            }
            return View(model);
        }

        

       [IgnoreRightFilter]
        public async Task<IActionResult> GetListWorkerWithPager(PaiBanFilter filters)
        {
            filters.type = "2";
            var result = await _iRepository.GetPaiBans(filters);
            foreach (var r in result.rows)
            {
                if (r.Worker != null)
                    r.WorkerName = r.Worker.Name;
                r.BanciName = r.BanCi.Name;
                foreach (var time in r.BanCi.TimePeriods)
                {
                    r.TimePeriods += $"{time.TimeStart}-{time.TimeEnd},";
                }
            }
            return Json(result);
        }

        [IgnoreRightFilter]
        public async Task<IActionResult> GetListWithPager(PaiBanFilter filters)
        {
            filters.type = "1";
            var result = await _iRepository.GetPaiBans(filters);
            foreach(var r in result.rows)
            {
                if(r.Worker != null)
                    r.WorkerName = r.Worker.Name;
                r.BanciName = r.BanCi.Name;
                foreach (var time in r.BanCi.TimePeriods)
                {
                    r.TimePeriods += $"{time.TimeStart}-{time.TimeEnd},";
                }
            }
            return Json(result);
        }
    }
}